package com.mijie.homi.search.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mijie.homi.common.domain.SearchResult;
import com.mijie.homi.common.domain.mood.Mood;
import com.mijie.homi.search.service.index.TopicIndexService;
import com.mijie.homi.search.service.mood.MoodService;
import com.mijie.homi.search.service.topic.ClassifyService;
import com.mijie.util.IOUtil;

@Controller
@RequestMapping("/topic")
public class TopicSearchApi {

	@Autowired
	private TopicIndexService service;
	@Autowired
	private MoodService moodService;
	@Autowired
	private ClassifyService classifyService;
	
	private Logger logger = Logger.getLogger(TopicSearchApi.class);
	
	@RequestMapping(value="/reindex",method=RequestMethod.GET)
	public @ResponseBody void reindex() throws Exception{
		service.reIndex();
	}
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public @ResponseBody void RAMIndex(int topicId) throws Exception{
		service.RAMIndex(topicId);
	}
	
	@RequestMapping(value="/search",method=RequestMethod.GET)
	public void searchByKeyword(String idxf,String keyword,int offset,int limit,HttpServletResponse response) throws Exception{
		SearchResult result = service.searchByKeyword(idxf, keyword, offset, limit);
		IOUtil.write(response, result.toJSON());
	}
	
	@RequestMapping(value="/search/top",method=RequestMethod.GET)
	public void listTopicOfTop(String idxf,int mood,int offset,int limit,HttpServletResponse response) throws Exception{
		SearchResult result = service.listTopicOfTop(idxf, mood, offset, limit);
		IOUtil.write(response, result.toJSON());
	}
	
	@RequestMapping(value="/search/mood_match",method=RequestMethod.GET)
	public void listTopicOfMoodMatch(String idxf,int mood,int offset,int limit,HttpServletResponse response) throws Exception{
		SearchResult result = service.listTopicOfMoodMatch(idxf, mood, offset, limit);
		IOUtil.write(response, result.toJSON());
	}
	
	@RequestMapping(value="/search/new",method=RequestMethod.GET)
	public void listTopicOfNew(String idxf,int mood,int offset,int limit,HttpServletResponse response) throws Exception{
		SearchResult result = service.listTopicOfNew(idxf, mood, offset, limit);
		IOUtil.write(response, result.toJSON());
	}
	
	@RequestMapping(value="/search/active",method=RequestMethod.GET)
	public void listTopicOfActive(String idxf,int mood,int offset,int limit,HttpServletResponse response) throws Exception{
		SearchResult result = service.listTopicOfActive(idxf, mood, offset, limit);
		IOUtil.write(response, result.toJSON());
	}
	
	@RequestMapping(value="/search/hot",method=RequestMethod.GET)
	public void listTopicOfHot(String idxf,int mood,int offset,int limit,HttpServletResponse response) throws Exception{
		SearchResult result = service.listTopicOfHot(idxf, mood, offset, limit);
		IOUtil.write(response, result.toJSON());
	}
	
	@RequestMapping(value="/search/match",method=RequestMethod.GET)
	public void listTopicOfMatch(String idxf,int userId,int offset,int limit,HttpServletResponse response) throws Exception{
		SearchResult result = null;
		Mood mood = moodService.getRecentlyMood(userId);
		if(mood!=null && !mood.getContent().isEmpty()){
			StringTokenizer tokenizer = new StringTokenizer(mood.getContent(),",，.。?？!！");
			List<Set<String>> keywordGroup = new ArrayList<Set<String>>();
			String token = null;
			while(tokenizer.hasMoreTokens()){
				token = tokenizer.nextToken();
				Set<String> keywords = classifyService.getKeywords(token);
				if(!keywords.isEmpty()){
					keywordGroup.add(keywords);
				}
			}
				 
			if(!keywordGroup.isEmpty()){
				String type = classifyService.classify(mood.getContent());
				result = service.smartMatch(idxf, keywordGroup, type, offset, limit);
				IOUtil.write(response, result.toJSON());
				return;
			}
			
		}
		SearchResult empty = new SearchResult();
		empty.setResult(new JSONArray());
		IOUtil.write(response, empty.toJSON());
	}
}
